#include "stdafx.h"
#include "encrypt_present.h"
#include "oneround.h"
#include "addroundkey.h"

#include <ctime>
#include <cstdlib>

void encrypt_present(  unsigned char* plaintext, unsigned char out_encrypt[], unsigned char* master_key)
{
	unsigned char *in_encryption = plaintext;
	unsigned char *encryption_key = master_key;

	unsigned char out_encryption[64] ;
	int num=0 ;

	unsigned char key[32][80]  ;
	unsigned char out_oneround[32][64] ;
	unsigned char out_result[32][16] ;
	/*	unsigned char ciphertext[64] ;*/
	unsigned char ciphertext_hex[16] ;
	unsigned char lastkey[64] ;
 
	for ( int i = 0 ; i <=63 ; i++ )
	{
		out_oneround[0][i] = in_encryption[i] ;
	}

	for ( int i = 0 ; i <= 79 ; i++ )
	{
		key[0][i] = encryption_key[i] ;
	}


	for ( int i = 1 ; i <= 31 ; i++ )
	{	
/*		printf( "round %d :   " , i ) ;*/
		OneRound(out_oneround[i-1] , key[i-1] , i , out_oneround[i] , key[i] ) ;
	}
	for ( int i = 0 ; i <= 63 ; i++ )
	{
		lastkey[i] = key[31][i+16] ;
	}
	AddRoundKey( out_oneround[31] , lastkey , out_encrypt ) ;

	for ( int i = 0 ; i <= 31 ; i++ )
	{	
		for ( int j = 0 ; j <= 15 ; j++ )
		{	
			out_result[i][j] = out_oneround[i][4*j] | (out_oneround[i][4*j+1]<<1) | (out_oneround[i][4*j+2]<<2) | (out_oneround[i][4*j+3]<<3) ;
		}
	}
	for ( int i = 0 ; i <= 15 ; i++ )
	{	
		ciphertext_hex[i] = out_encrypt[4*i] | (out_encrypt[4*i+1]<<1) | (out_encrypt[4*i+2]<<2) | (out_encrypt[4*i+3]<<3) ;
	}
// 	for ( int i = 0 ; i <= 63 ; i++ )
// 	{
// 		printf( "%x" , lastkey[i] ) ;
// 		if ( (i+1)%8 == 0 )
// 		{
// 			printf( " " ) ;
// 		}
// 	}
// 	printf("\n");
//  	for ( int i = 15 ; i >= 0 ; i-- )
//  	{
//  		printf( "%x" , ciphertext_hex[i] ) ;
//  	}

}